package Logic;

import irio.gamer.gamer;

import java.util.ArrayList;

import utils.GamerException;
import utils.IniFile;
import utils.Vec;

public class Galaxy {

	public String filename = "";
	public Vec orientation = new Vec(0,0,1);
	public Vec axis = new Vec(1,1,1);
	public Vec bulge_axis = new Vec(1,1,1);
//	public Vec orientation = new Vec();
	public Vec no_arms = new Vec(2,0,0);
	public Vec arm1 = new Vec(0,0,0);
	public Vec arm2 = new Vec(3.14159,0,0);
	public Vec arm3 = new Vec(0,0,0);
	public Vec arm4 = new Vec(0,0,0);
	public Vec bulge_dust = new Vec(0.015);
	public Vec innerTwirl = new Vec(1,0,0);
	public Vec winding_N = new Vec(2,0,0);
	public Vec winding_B = new Vec(0.5,0,0);
	public Vec no_components = new Vec(0,0,0);

	public Vec warpScale = new Vec(0,0,0);
	public Vec warpAmplitude = new Vec(0,0,0);
	
	
	public ArrayList<GalaxyComponent> components = new ArrayList<GalaxyComponent>();
	
	
	public boolean initialized = false;
	
	public void CopyFrom(Galaxy g) {
		axis.set(g.axis);
		bulge_axis.set(g.bulge_axis);
		no_arms.set(g.no_arms);
		arm1.set(g.arm1);
		arm2.set(g.arm2);
		arm3.set(g.arm3);
		arm4.set(g.arm4);
		bulge_dust.set(g.bulge_dust);
		innerTwirl.set(g.innerTwirl);
		winding_N.set(g.winding_N);
		winding_B.set(g.winding_B);
		no_components.set(g.no_components);
		warpScale.set(g.warpScale);
		warpAmplitude.set(g.warpAmplitude);
		components.clear();
		for (GalaxyComponent gc: g.components) {
			GalaxyComponent c = new GalaxyComponent();
			c.active = gc.active;
			c.armstrength.set(gc.armstrength);
			c.component_no = gc.component_no;
			c.delta.set(gc.delta);
			c.intensity_modifier.set(gc.intensity_modifier);
			c.noiseOffset.set(gc.noiseOffset);
			c.noiseTilt.set(gc.noiseTilt);
			c.r0.set(gc.r0);
			c.z0.set(gc.z0);
			c.scale.set(gc.scale);
			c.spectrum.set(gc.spectrum);
			c.type.set(gc.type);
			c.winding.set(gc.winding);
			components.add(c);
		}
	}
	
	
	void CGalaxy(String fname) {
		Load(fname);
	}
	 
	public void Load(String fname) {
		filename = fname;
		try {
		IniFile ini = new IniFile(gamer.gamerParams.galaxyDir + fname);
		components.clear();
		axis = ini.getVec("axis");
		bulge_axis = ini.getVec("bulge_axis");
		//orientation = ini.getVec("orientation");
		no_arms.x = ini.getDouble("no_arms");
		try {
			arm1.x = ini.getDouble("arm1");
		}
		catch (Exception e) {};
		try {
			arm2.x = ini.getDouble("arm2");
		}
		catch (Exception e) {};
		try {
			arm3.x = ini.getDouble("arm3");
		}
		catch (Exception e) {};
		try {
			arm4.x = ini.getDouble("arm4");
		}
		catch (Exception e) {};
		//bulge_size.x = ini.getDouble("bulge_size");
		bulge_dust.x = ini.getDouble("bulge_dust");
		innerTwirl.x = ini.getDouble("innerTwirl");

		winding_N.x = ini.getDouble("winding_N");
		winding_B.x = ini.getDouble("winding_B");

		no_components.x = ini.getDouble("no_components");
		for (int i= 0;i<(int)no_components.x;i++) {
			GalaxyComponent gc = new GalaxyComponent();
			gc.Load(ini, i);
			components.add(gc);
		}
		initialized = true;
	
		warpAmplitude.x = ini.getDouble("warp_amplitude");
		warpScale.x = ini.getDouble("warp_scale");

		
		} catch (GamerException ge) {
			gamer.HandleWarning(ge, "Error loading " + fname);
			gamer.loadGalaxy();
		}
			
	}

	public void RemoveComponent(GalaxyComponent gc) {
		components.remove(gc);
		for (int i=0;i<components.size();i++)
			components.get(i).component_no = i+1;
	}
	
	public void Save(String fname) {

		if (fname.equals("") || initialized == false)
			return;

		IniFile ini = new IniFile();
		ini.addKey("axis", axis);
		ini.addKey("bulge_axis", bulge_axis);
//		ini.addKey("orientation", orientation);
		ini.addKey("no_arms", (int)no_arms.x);
		ini.addKey("arm1", arm1.x);
		ini.addKey("arm2", arm2.x);
		ini.addKey("arm3", arm3.x);
		ini.addKey("arm4", arm4.x);
		//ini.addKey("bulge_size", bulge_size.x);
		ini.addKey("bulge_dust", bulge_dust.x );
		ini.addKey("innerTwirl", (int)innerTwirl.x);
		ini.addKey("winding_N", winding_N.x );
		ini.addKey("winding_B", winding_B.x );
		no_components.x = components.size();
		ini.addKey("no_components", (int)no_components.x);

		ini.addKey("warp_scale", warpScale.x);
		ini.addKey("warp_amplitude", warpAmplitude.x);

		
		
		for (GalaxyComponent gc : components) {
			gc.Save(ini);
		}

		ini.Save(gamer.gamerParams.galaxyDir + fname);
	}
	
}
